package com.kang.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kang.CommonsUtils;
import com.kang.Constants;
import com.kang.MsgResult;
import com.kang.bean.bot.City;
import com.kang.bean.bot.MapType;
import com.kang.bean.bot.WarMap;
import com.kang.manager.BotAutoManager;
import com.kang.service.warMap.CityService;
import com.kang.service.warMap.MapTypeService;
import com.kang.service.warMap.WarMapService;
import com.kang.vo.CityVo;
import lombok.extern.slf4j.Slf4j;
import love.forte.common.ioc.annotation.Beans;
import love.forte.common.ioc.annotation.Depend;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.containers.GroupAccountInfo;
import love.forte.simbot.api.message.containers.GroupInfo;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.filter.MatchType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: service
 * @description: 机器人游戏回答监听类
 * @author: K.faWu
 * @create: 2022-06-09 10:22
 **/
@Slf4j
@Beans
@Component
public class PlayListener {
    /**
     * 通过依赖注入获取一个 "消息正文构建器工厂"。
     *
     */
    @Autowired
    private CityService cityService;
    @Autowired
    private WarMapService warMapService;
    @Autowired
    private MapTypeService mapTypeService;
    @Depend
    @Autowired
    private BotAutoManager botAutoManager;

    /**
     * 帮助
     */
    @Filter(value = ".help", matchType = MatchType.EQUALS)
    @OnGroup
    public void help(GroupMsg groupMsg, Sender sender){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        sender.sendGroupMsg(groupCode, MsgResult.HELP);
    }

    /**
     * 更新公告
     */
    @Filter(value = "更新公告",matchType = MatchType.EQUALS)
    @OnGroup
    public void notice(GroupMsg groupMsg, Sender sender){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        sender.sendGroupMsg(groupCode, MsgResult.NOTICE);
    }
    /**
     * 建立城池
     */
    @Filter(value = "建城", matchType = MatchType.EQUALS)
    @OnGroup
    public void buildCity(GroupMsg groupMsg, Sender sender){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountNickname = accountInfo.getAccountNickname();
        //请求人账号
        String accountCode = accountInfo.getAccountCode();
        //判断是否已经存在
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.eq("account_code", accountCode);
        wrapper.eq("group_code", groupCode);
        List<City> list = cityService.list(wrapper);

        if (list != null && list.size() != 0){
            sender.sendGroupMsg(groupCode, MsgResult.repeatInitCity(accountNickname));
        }else{
            //判断是否有地图
            List<WarMap> warMapList = warMapService.initWarMap(groupCode);
            //建立城邦信息
            cityService.initCity(accountCode, groupCode, warMapList);

            sender.sendGroupMsg(groupCode, MsgResult.initCity(accountNickname));
            //展示城邦信息
            getCityInfo(groupMsg, sender);
        }
    }

    /**
     * 查看信息
     */
    @Filter(value = "查看城邦", matchType = MatchType.EQUALS)
    @OnGroup
    public void getCityInfo(GroupMsg groupMsg, Sender sender){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountCode = accountInfo.getAccountCode();
        String accountNickname = accountInfo.getAccountNickname();

        //查询个人城邦信息
        CityVo cityVo = cityService.getCityInfo(accountCode, groupCode);
        if (cityVo!=null){
            String msg = MsgResult.getCityInfo(accountNickname, cityVo);
            sender.sendGroupMsg(groupCode, msg);
        }else{
            sender.sendGroupMsg(groupCode, MsgResult.NOT_CITY);
        }
    }

    /**
     * 城池+ (x,y)信息
     */
    @OnGroup
    @Filter(value = "城池", matchType = MatchType.STARTS_WITH)
    public void getWarMapInfo(GroupMsg groupMsg, Sender sender){
        String accountCode = groupMsg.getAccountInfo().getAccountCode();
        String accountNickname = groupMsg.getAccountInfo().getAccountNickname();
        String groupCode = groupMsg.getGroupInfo().getGroupCode();
        String text = groupMsg.getText();
        String xStr = text.substring(text.indexOf("（") + 1, text.indexOf("，"));
        String yStr = text.substring(text.indexOf("，") + 1, text.indexOf("）"));
        QueryWrapper<WarMap> queryWrapper = CommonsUtils.selectCode(accountCode, groupCode);
        queryWrapper.eq("x", xStr);
        queryWrapper.eq("y", yStr);
        WarMap warMap = warMapService.getOne(queryWrapper);
        if (warMap == null){
            //区域不属于城主，无法查看
            sender.sendGroupMsg(groupCode, MsgResult.NOT_HAVE_WAR_MAP);
            return;
        }
        List<MapType> typeList = mapTypeService.getAll();
        Map<String, String> typeMap = new HashMap<>(4);
        for (MapType type: typeList){
            typeMap.put(type.getTypeCode(), type.getName());
        }
        if (text.contains(Constants.RECRUIT) || text.contains(Constants.DISBAND)) {
            //招募/遣散
            recruitOrDisband(groupMsg, sender, warMap);
        } else if (text.contains(Constants.ADD)){
            addPro(groupMsg, sender, warMap);
        } else {
            String msg = MsgResult.warMapInfo(accountNickname, warMap, -1, typeMap);
            sender.sendGroupMsg(groupCode, msg);
        }
    }

    /**
     * 招募/遣散 ,指令为”城池（x，y）招募/遣散**num“，后面的数字表示招募数量
     */
    public void recruitOrDisband(GroupMsg groupMsg, Sender sender, WarMap warMap){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountNickname = accountInfo.getAccountNickname();

        String str, type, text = groupMsg.getText();
        //判断招募的类型
        if (text.contains(Constants.SOLDIER)){
            type = Constants.SOLDIER;
        }else if (text.contains(Constants.MINER)){
            type = Constants.MINER;
        }else if (text.contains(Constants.WOODCUTTER)){
            type = Constants.WOODCUTTER;
        }else {
            sender.sendGroupMsg(groupCode, MsgResult.ERROR);
            return;
        }

        //要招募的数量
        str = text.substring(text.indexOf(type.charAt(type.length()-1)) + 1).trim();

        //判断是否招募成功的逻辑
        if (!"".equals(str)){
            Integer num;
            try {
                 num = new Integer(str);
            }catch (Exception e){
                e.printStackTrace();
                //所输入不是数字
                sender.sendGroupMsg(groupCode, MsgResult.ERROR);
                return;
            }
            String msg = "";
            if (text.contains(Constants.RECRUIT)) {
                // 招募， 人员分配变更，资源变更
                msg = cityService.updateByRecruit(groupMsg, num, type, warMap);
            } else if (text.contains(Constants.DISBAND)) {
                //遣散
                msg = cityService.updateByDisband(groupMsg, num, type, warMap);
            }
            sender.sendGroupMsg(groupCode, msg);
        }else{
            //未填写招募数量
            sender.sendGroupMsg(groupCode, MsgResult.notNum(accountNickname));
        }
    }

    /**
     * 增加人口，消耗粮食增加人口
     */
    public void addPro(GroupMsg groupMsg, Sender sender, WarMap warMap){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountCode = accountInfo.getAccountCode();
        String accountNickname = accountInfo.getAccountNickname();

        String text = groupMsg.getText();
        String str = text.substring(text.indexOf("民")+1).trim();
        if (!"".equals(str)){
            Integer proNum = new Integer(str);
            String msg = warMapService.addPro(accountCode, groupCode, proNum, accountNickname, warMap);
            sender.sendGroupMsg(groupCode, msg);
        }else {
            sender.sendGroupMsg(groupCode, MsgResult.notNum(accountNickname));
        }

    }

    /**
     * 搜寻领土 --持续会话
     */

    @Filter(value = "刷新物资", matchType = MatchType.STARTS_WITH)
    @OnGroup
    public void test(GroupMsg groupMsg, Sender sender){
        GroupInfo groupInfo = groupMsg.getGroupInfo();
        String groupCode = groupInfo.getGroupCode();
        GroupAccountInfo accountInfo = groupMsg.getAccountInfo();
        String accountCode = accountInfo.getAccountCode();
        if (!"1551360792".equals(accountCode)){
            sender.sendGroupMsg(groupCode, "您无权刷新物资！");
            return;
        }

        sender.sendGroupMsg(groupCode, "开始刷新物资...");
        refresh();
        sender.sendGroupMsg(groupCode, "刷新物资完成");

    }

    public void refresh() {
        //更新数据
        Map<String, List<String>> map =  cityService.refresh();
        /*
         * 通知玩家粮食不够
         */
        botAutoManager.sendByGroupAll(map);
    }
}
